package com.zp.business.controller.template;

import com.zp.business.entity.template.bo.TemplateMasterBo;
import com.zp.business.entity.template.vo.TemplateMasterVo;
import com.zp.business.service.template.ITemplateMasterService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 模板主记录控制器
 * 前端访问路由地址为:/business/templateMaster
 *
 * @author zhang peng
 * @ date 2024-04-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/templateMaster")
public class TemplateMasterController extends BaseController {

    private final ITemplateMasterService iTemplateMasterService;

    /**
     * 查询模板主记录列表 (分页)
     */
//    @SaCheckPermission("business:templateMaster:list")
    @GetMapping("/selectPage")
    public TableDataInfo<TemplateMasterVo> selectPage (TemplateMasterBo bo, PageQuery pageQuery) {
        return iTemplateMasterService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询模板主记录列表 (不分页)
     */
//    @SaCheckPermission("business:templateMaster:list")
    @GetMapping("/list")
    public R<List<TemplateMasterVo>> list (TemplateMasterBo bo) {
        List<TemplateMasterVo> list = iTemplateMasterService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出模板主记录列表
     */
//    @SaCheckPermission("business:templateMaster:export")
//    @Log(title = "模板主记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (TemplateMasterBo bo, HttpServletResponse response) {
        List<TemplateMasterVo> list = iTemplateMasterService.queryList(bo);
        ExcelUtil.exportExcel(list, "模板主记录", TemplateMasterVo.class, response);
    }

    /**
     * 获取模板主记录详细信息
     *
     * @param templateMasterId 主键
     */
//    @SaCheckPermission("business:templateMaster:list")
    @GetMapping("/{templateMasterId}")
    public R<TemplateMasterVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String templateMasterId) {
        return R.ok(iTemplateMasterService.queryById(templateMasterId));
    }

    /**
     * 新增模板主记录
     */
//    @SaCheckPermission("business:templateMaster:add")
    @Log(title = "模板主记录", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody TemplateMasterBo bo) {
        return toAjax(iTemplateMasterService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改模板主记录
     */
//    @SaCheckPermission("business:templateMaster:edit")
    @Log(title = "模板主记录", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody TemplateMasterBo bo) {
        return toAjax(iTemplateMasterService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除模板主记录
     *
     * @param templateMasterIds 主键串
     */
//    @SaCheckPermission("business:templateMaster:remove")
    @Log(title = "模板主记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateMasterIds}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] templateMasterIds) {
        return toAjax(iTemplateMasterService.deleteWithValidByIds(Arrays.asList(templateMasterIds), true) ? 1 : 0);
    }
}
